//
//  RYQuerenRenCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/13.
//  Copyright © 2021 叁拾叁. All rights reserved.
//




import UIKit

class RYQuerenRenCell: SYBaseCell , UITableViewDelegate, UITableViewDataSource{
    
    var tableView: UITableView!
    
    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5
        v.layer.masksToBounds = true
        return v
    }()

    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1D1D1D)
        v.font = 16.autoBoldFontSize()
        v.text = "认养人信息"
        return v
    }()
    
    override func initViews() {
        
        setTableView()
        self.backgroundColor = Constant.bgViewColor
        
        contentView.addSubview(bgView)
        
        bgView.addSubview(nameLb)
        bgView.addSubview(tableView)
        
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.autoWidth())
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(30.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(126.autoWidth())
            make.bottom.equalToSuperview()
        }
    }

    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.isScrollEnabled = false
        self.tableView!.register(RYQuerenRenSubCell.self, forCellReuseIdentifier: "Cell")
        self.contentView.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: RYQuerenRenSubCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? RYQuerenRenSubCell
        if indexPath.row == 0 {
            cell?.title.text = "用户姓名"
            cell?.content.placeholder = "请输入用户姓名"
            cell?.passParamsClosure = {[weak self]  param in
                if let `self` = self {
                    let currentvc = (self.currentVC as! RYQuerenDIngdanViewController)
                    currentvc.params["adoptUserName"] = param as? String ?? ""
                }
                
            }
        } else if indexPath.row == 1 {
            cell?.title.text = "联系方式"
            cell?.content.placeholder = "请输入联系方式"
            cell?.passParamsClosure = {[weak self]  param in
                if let `self` = self {
                    let currentvc = (self.currentVC as! RYQuerenDIngdanViewController)
                    currentvc.params["adoptUserContact"] = param as? String ?? ""
                }
                
            }
        } else {
            cell?.title.text = "用户备注"
            cell?.content.placeholder = "请输入备注信息"
            cell?.passParamsClosure = {[weak self]  param in
                if let `self` = self {
                    let currentvc = (self.currentVC as! RYQuerenDIngdanViewController)
                    currentvc.params["remarks"] = param as? String ?? ""
                }
                
            }
        }
        cell?.selectionStyle = .none
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt")
    }
}




class RYQuerenRenSubCell: SYBaseCell {
    


    
    let title: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 15.autoFontSize()
        
        return v
    }()
    
    let content: UITextField = {
        let v:UITextField = UITextField()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        v.textAlignment = .right
        return v
    }()
    
    override func initViews() {
        
        //当文本框内容改变时，将内容输出到控制台上
        content.rx.text.orEmpty.asObservable()
            .subscribe(onNext: {[weak self] in
                if let `self` = self {
                    self.passParamsClosure?($0)
                }
                print("您输入的是：\($0)")
                
            })
            .disposed(by: disposeBag)
        
            contentView.addSubview(lineView)
        contentView.addSubview(title)
        contentView.addSubview(content)
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(0.5.autoWidth())
            make.bottom.equalToSuperview()
        }
        title.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
            make.height.equalTo(18.autoWidth())
        }
        content.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-30)
            make.centerY.equalToSuperview()
            make.height.equalTo(15.autoWidth())
        }
    }

}
